package pri.syl.xiaowu.websocket.autoconfigure;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Objects;

/**
 * ClassName: WebsocketProperties
 * Description: Websocket配置属性
 */
@Data
@ConfigurationProperties(prefix = WebsocketProperties.WEBSOCKET_PREFIX)
public class WebsocketProperties {
    public static final String WEBSOCKET_PREFIX = "websocket";
    private String name;
    /**
     * ServerUrl
     * 服务Url
     */
    private String url;
    /**
     * MaxFramePayloadLength
     * 最大帧载荷长度
     */
    private int maxFramePayloadLength = 65536;
    /**
     * ReadTimeoutMilli
     * 读取超时
     */
    private int readTimeoutMilli = 16000;


    @Value("${server.port}")
    private Integer serverPort;

    private Integer port;

    public String getUrl() {
        if (url.contains(":")) {
            return url;
        }
        Integer changeProt = getPortOrDefault();
        return url.concat(":" + changeProt.toString());
    }

    public Integer getPortOrDefault() {
        return Objects.isNull(port) ? serverPort + 500 : port;
    }
}
